//
//  JsonUtil.m
//  GasEasyPay
//
//  Created by 张宁宁 on 16/4/8.
//  Copyright © 2016年 lirenkj. All rights reserved.
//

#import "JsonUtil.h"

@implementation JsonUtil
/**
 *  转为JSON字符串
 *
 *  @param theData 对象
 *
 *  @return JSON字符串
 */
+(NSString *)toJSONData:(id)theData{
    
    NSError *error = nil;
    if(theData==nil)
        return  @"空";
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    if ([jsonData length] > 0 && error == nil){
        return [[NSString alloc] initWithData:jsonData
                                     encoding:NSUTF8StringEncoding];
    }else{
        return @"Json异常或Json为空";
    }
}
/**
 *  JSON字符串转为字典
 *
 *  @param jsonStr JSON字符串
 *
 *  @return 字典
 */
+ (NSDictionary *)dictinoryWithJsonString:(NSString *)jsonStr{
    if (!jsonStr) {
        return nil;
    }
    NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    if (error) {
        return nil;
    }
    return dic;
}

/**
 *  JSON字符串转为数组
 *
 *  @param jsonStr JSON字符串
 *
 *  @return 数组
 */
+ (NSArray *)arrayWithJsonString:(NSString *)jsonStr{
    if (!jsonStr) {
        return nil;
    }
    NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSArray *array = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    if (error) {
        return nil;
    }
    return array;
}

@end
